-- cross_trap
-- create by panyl
-- 十字型陷阱

return {
    getTargets = function(trapId, pos, targetPos)
        local info = CommonTrapM.query(trapId);
        local trapType = info["type"];
        local feature = info["feature"];

        assert(trapType == TRAP_TYPE_CROSS, "不是十字星的陷阱！");

        local radius = info["radius"];
        local maxRadius = math.max(DUNGEON_HEIGHT, DUNGEON_WIDTH);
        if type(radius) ~= "number" or
            radius == NUMBER_INFINITY or
            radius > maxRadius then
            radius = maxRadius;
        end

        local ret = {};
        local directions = { "left", "right", "up", "down", };
        local coor = DungeonM.convertToCoor(pos);
        local x = coor["x"];
        local y = coor["y"];

        -- 一层层遍历
        local stat = {};
        for dist = 1, radius do
            -- 遍历四个方向
            for _, direction in pairs(directions) do
                repeat
                    local vector = CommonTrapM.getVector(direction);

                    local xx = x + vector[1] * dist;
                    local yy = y + vector[2] * dist;

                    -- 超出边界了
                    if xx <= 0 or xx > DUNGEON_WIDTH or yy <= 0 or yy > DUNGEON_HEIGHT then
                        break;
                    end

                    pos = DungeonM.convertToPos(xx, yy);
                    local grid = DungeonM.getGridByPos(pos);
                    local monster = grid.monster;

                    -- 是否无视障碍的
                    if  not grid:isOpened() and
                        bit.band(feature, TRAP_FEATURE_IGNORE_BLOCK) ~= TRAP_FEATURE_IGNORE_BLOCK then
                        break;
                    end
                    
                    -- 如果是不可穿透的（只能攻击最前面的敌人）
                    stat[direction] = stat[direction] or {};
                    if (#stat[direction] > 0 and
                        bit.band(feature, TRAP_FEATURE_NOT_THROUGH) == TRAP_FEATURE_NOT_THROUGH) then
                        break;
                    end

                    if  not grid:isMonster() or
                        not monster or
                        monster:isDead() then
                        break;
                    end

                    table.insert(ret, grid);

                    table.insert(stat[direction], grid);
                until true;
            end
        end

        return ret;
    end,
};
